package br.gov.dataprev.treinamento.ejb.modelo;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;

import br.gov.dataprev.treinamento.ejb.validacao.Email;
import br.gov.dataprev.treinamento.ejb.validacao.grupos.Alterar;
import br.gov.dataprev.treinamento.ejb.validacao.grupos.Incluir;
import br.gov.dataprev.treinamento.ejb.validacao.grupos.PK;

@Entity
@Table(name="CLIENTES",uniqueConstraints={@UniqueConstraint(columnNames={"NO_EMAIL"})})
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="CS_CLIENTE", discriminatorType=DiscriminatorType.STRING, length=2)
public abstract class Cliente implements Serializable {
	
	private static final long serialVersionUID = 1L;

	//JPA
	@Id
	@SequenceGenerator(name="SQ_CLIENTES", initialValue=1, allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQ_CLIENTES")
	@Column(name="ID_CLIENTE")
	//VALIDATION
	@NotNull(groups={Alterar.class, Default.class, PK.class})
	@Null(groups={Incluir.class})
	private Long id;

	//JPA
	@Column(name="NO_EMAIL", length=50, nullable=false)
	//VAlIDATION
	@NotNull(groups={Default.class, Incluir.class})
	@Email(groups={Default.class, Incluir.class})
	private String email;
	
	//JPA
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="DT_CADASTRO", updatable=false, nullable=false)
	//VALIDATION
	@NotNull
	@Past(groups={Default.class, Incluir.class})
	private Date dataCadastro;
	
	//JPA
	@OneToMany(fetch=FetchType.EAGER, orphanRemoval=true, cascade={CascadeType.ALL}, mappedBy="cliente")
	//VALIDATION
	@Size(min=0, max=3)
	private List<Telefone> telefones = new ArrayList<Telefone>();
	
	//JPA
//	@OneToOne(mappedBy="cliente", cascade={CascadeType.ALL}/*, fetch=FetchType.EAGER*/)
	@ManyToOne(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
	@JoinColumn(name="ID_ENDERECO")
	//VALIDATION
	@NotNull(groups={Default.class, Incluir.class})
	@Valid
	private Endereco endereco;
	
	public Cliente() {
		// TODO Auto-generated constructor stub
	}


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}

	public List<Telefone> getTelefones() {
		return telefones;
	}

	public void setTelefones(List<Telefone> telefones) {
		this.telefones = telefones;
	}
	
	public void addTelefone(Telefone t) {
		telefones.add(t);
		t.setCliente(this);
	}

	public Endereco getEndereco() {
		return endereco;
	}

	public void setEndereco(Endereco endereco) {
		this.endereco = endereco;
//		if(endereco != null) {
//			endereco.setCliente(this);
//		}
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Cliente))
			return false;
		Cliente other = (Cliente) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
